import java.util.HashMap;
import java.util.Map;

public class demo {
}
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        Map<Integer,int[]> map = new HashMap<>();
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                map.put(mat[i][j],new int[]{i,j});
            }
        }
        int[] a = new int[m];
        int[] b = new int[n];
        int index=0;
        for(; index<m*n; index++){
            int[] tmp = map.get(arr[index]);
            int i = tmp[0], j = tmp[1];
            System.out.println(i+" " + j);
            if((++a[i])==n){
                return index;
            }
            if((++b[j])==m){
                return index;
            }
        }
        return -1;
    }
}
